﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Reflection
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Type personType = typeof(Person);
                object[] constructorParams = { "Jane", 18 };
                ConstructorInfo constructor = personType.GetConstructor(new Type[] { typeof(string), typeof(int) });
                // 使用反射调用构造函数创建实例
                object personInstance = constructor.Invoke(constructorParams);
                Person person = (Person)personInstance;
                Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    }
}
